package android.mobilin;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.app.AlertDialog.Builder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RequestActivity extends Activity implements OnClickListener, android.content.DialogInterface.OnClickListener {
	Button request;
	Button back;
	EditText caseNo;
	EditText pinNo;
	Builder builder;
	AlertDialog confirm;
	String msg="";
	PendingIntent pi;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.request);
		request=(Button)findViewById(R.id.sendReq);
		back=(Button)findViewById(R.id.backReq);
		caseNo=(EditText)findViewById(R.id.caseno);
		pinNo=(EditText)findViewById(R.id.pin);
		request.setOnClickListener(this);
		back.setOnClickListener(this);
		builder = new Builder(this);
		builder.setCancelable(false)
		       .setPositiveButton("Yes", this)
		       .setNegativeButton("Cancel", this);
		String SENT = "SMS_SENT";
        pi = PendingIntent.getBroadcast(this, 0,
            new Intent(SENT), 0);
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "Request sent", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(), "Generic failure", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(), "No service", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getBaseContext(), "Null PDU", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getBaseContext(), "Radio off", 
                                Toast.LENGTH_SHORT).show();
                        break;
                }
            }
			
        }, new IntentFilter(SENT));

	}

	public void onClick(View v) {
		if(v==request) {
			String caseNum=caseNo.getText().toString();
			String pin=pinNo.getText().toString();
			if(caseNum.length()>0 && pin.length()>0) {
				builder.setMessage("Request referral?\nSMS charges apply.");
				confirm=builder.show();
			}
			else {
				Toast.makeText(this, "Please fill up all fields.", Toast.LENGTH_SHORT).show();
			}
		}
		else if(v==back) {
			finish();
		}
	}

	public void onClick(DialogInterface dialog, int which) {
		if(dialog==confirm) {
			switch(which) {
				case DialogInterface.BUTTON_POSITIVE: 
					String caseNum=caseNo.getText().toString();
					String pin=pinNo.getText().toString();
					if(caseNum.length()>0 && pin.length()>0) {
						StartActivity.sendSMS(null,"REQ\0"+caseNum+"\0"+pin, pi,true);
					}
					break;
				case DialogInterface.BUTTON_NEGATIVE:
					confirm.dismiss();
					break;
			}
		}
		
	}
	
	
}
